package com.kyleswebspace.propertyanalyzer.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.kyleswebspace.parser.FieldParser;
import com.kyleswebspace.propertyanalyzer.config.ArcaFont;
import com.kyleswebspace.propertyanalyzer.config.ColorBox;
import com.kyleswebspace.propertyanalyzer.model.DataModel;

public class RentFrame extends JFrame implements Updateable, KeyListener {

	private JPanel outterPanel;
	private JPanel innerPanel;
	private JLabel aptsLabel;
	private JLabel monthlyLabel;
	private JLabel yearlyLabel;
	private JScrollPane scrollPane;

	private JLabel yearlyTotalLabel;
	private JLabel monthlyTotal;
	private JLabel yearlyTotal;
	
	private DataModel dm;
	private FocusMediator fm;
	
	private GridBagConstraints constraints;
	
	public RentFrame(DataModel dm, FocusMediator fm) {
		this.dm = dm;
		this.fm = fm;
		
		innerPanel = new JPanel();
		innerPanel.setLayout(new GridBagLayout());
		innerPanel.setBackground(ColorBox.innerPanelColor);
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		
		//create labels and fields
		for(int i = 0; i < 50; i++) {

			constraints.gridx = 0;
			constraints.gridy = i;
			constraints.gridwidth = 1;
			constraints.insets = new Insets(0, 0, 0, 20);
			innerPanel.add(dm.getNumberLabels().get(i), constraints);
			
			constraints.gridx = 5;
			constraints.gridy = i;
			constraints.gridwidth = 4;
			constraints.insets = new Insets(0, 0, 0, 20);
			dm.getMonthlyFields().get(i).addFocusListener(this);
			innerPanel.add(dm.getMonthlyFields().get(i), constraints);
			
			constraints.gridx = 9;
			constraints.gridy = i;
			constraints.gridwidth = 4;
			constraints.insets = new Insets(0, 0, 0, 0);
			dm.getYearlyFields().get(i).addFocusListener(this);
			innerPanel.add(dm.getYearlyFields().get(i), constraints);
		}
		
		
		outterPanel = new JPanel();
		outterPanel.setLayout(new GridBagLayout());
		outterPanel.setBackground(ColorBox.backgroundPanelColor);
		constraints = new GridBagConstraints();
		constraints.ipadx = 15;
		
		//Apts Label
		aptsLabel = new JLabel("Apt");
		aptsLabel.setFont(ArcaFont.appFont);
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(0, 6, 0, 6);
		outterPanel.add(aptsLabel, constraints);
		
		//Monthly Label
		monthlyLabel = new JLabel("Monthly");
		monthlyLabel.setFont(ArcaFont.appFont);
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(0, 0, 0, 3);
		outterPanel.add(monthlyLabel, constraints);
		
		//Yearly Label
		yearlyLabel = new JLabel("Yearly");
		yearlyLabel.setFont(ArcaFont.appFont);
		constraints.gridx = 2;
		constraints.gridy = 0;
		constraints.gridwidth = 1;
		constraints.insets = new Insets(0, 20, 0, 0);
		outterPanel.add(yearlyLabel, constraints);
		constraints.insets = new Insets(0, 0, 0, 0);
		
		scrollPane = new JScrollPane(innerPanel);
		scrollPane.setPreferredSize(new Dimension(200,400));
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.gridwidth = 6;
		outterPanel.add(scrollPane, constraints);
		
		constraints.gridx = 0;
		constraints.gridy = 2;
		outterPanel.add(dm.getYearlyRentLabel(), constraints);
		
		for(JTextField field: dm.getMonthlyFields()) {
			field.addKeyListener(this);
		}
		fm.setMonthlyRentFields(dm.getMonthlyFields());
		
		this.add(outterPanel);
		this.setTitle("Rents Payable");
		this.pack();
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		
		updateFields();
	}
	
	public void updateModel() {
		dm.updateInterface();
	}

	@Override
	public void updateFields() {
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		if (arg0.getSource() instanceof JTextField) {
			((JTextField)(arg0.getSource())).selectAll();
		}
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		updateModel();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		//wrapping code for top and bottom elements
		if (e.getSource() == dm.getMonthlyFields().get(0)) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getMonthlyRentFields().get(1).requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getMonthlyRentFields().get(dm.getMonthlyFields().size()-1).requestFocus();
			}
		} else if (e.getSource() == dm.getMonthlyFields().get(dm.getMonthlyFields().size()-1)) {
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getMonthlyRentFields().get(0).requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getMonthlyRentFields().get(dm.getMonthlyFields().size()-2).requestFocus();
			}
		} else {
			int currentIndex = fm.getMonthlyRentFields().indexOf(e.getSource());
			if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				fm.getMonthlyRentFields().get(currentIndex + 1).requestFocus();
			} else if (e.getKeyCode() == KeyEvent.VK_UP) {
				fm.getMonthlyRentFields().get(currentIndex - 1).requestFocus();
			}
		}
		
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			updateModel();
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}
}
